//package medicalApp.velociraptors.com;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
/**
 * GUI panel for the Nurse User
 * 
 * @author xiang feng
 */

@SuppressWarnings("serial")
public class NursePanel extends JPanel {

	private JTextPane lblNurseInfo;
	public JTable tblNursePatientList;
	private MedAppController mac;
	private JScrollPane scrollPane;
	protected Integer chosenSSN;
	private JButton btnLogout;
	private JLabel lblNewLabel;
	private JSeparator separator_2;
	private JButton btnLookup;
	private JButton btnDelete;
	private JButton btnAdd;
	private JSeparator separator_1;
	private JSeparator separator;
//	private int drSSN;
	private int nurseSSN;

	/**
	 * Create the panel.
	 * 
	 * @param mac The MedAppController object that governs the panel's operation.
	 */
	public NursePanel(final MedAppController mac) {
		this.mac = mac;
		setLayout(null);

		separator = new JSeparator();
		separator.setForeground(Color.GRAY);
		separator.setBounds(0, 100, 150, 2);
		add(separator);

		separator_1 = new JSeparator();
		separator_1.setOrientation(SwingConstants.VERTICAL);
		separator_1.setBounds(150, 0, 2, 600);
		add(separator_1);

		btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			public void actionPerformed(ActionEvent arg0) {
				AddPatientDialog adp = new AddPatientDialog(mac);
				adp.setVisible(true);
			}// End actionPerformed method for btnAdd
		});// End ActionListener addition
		btnAdd.setBounds(33, 182, 89, 23);
		add(btnAdd);

		btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			public void actionPerformed(ActionEvent arg0) {
				mac.out.println("chosenSSN: "+chosenSSN);
				if(chosenSSN!=null){
					int n = JOptionPane.showConfirmDialog(null, "Do you want to delete this patient?");
					if(n == JOptionPane.YES_OPTION){

						try {
							String sql = "DELETE FROM `patients` WHERE P_SSN = '"+chosenSSN+"'";
							mac.out.println(sql);
							String sql2 = "DELETE FROM `user` WHERE U_SSN = '"+chosenSSN+"'";
							mac.out.println(sql);
							Connection con = mac.getCon();
							Statement stmt = con.createStatement();
							int rs = stmt.executeUpdate(sql);
							int rs2 = stmt.executeUpdate(sql2);
							JOptionPane.showMessageDialog(null, "Patient Deleted!");
							mac.setDoctorPatientTableModel();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				else
					JOptionPane.showMessageDialog(null, "Select a Patient!");
			}// End actionPerformed method for btnDelete
		});// End ActionListener addition
		btnDelete.setBounds(33, 235, 89, 23);
		add(btnDelete);

		btnLookup = new JButton("Lookup");
		btnLookup.addActionListener(new ActionListener() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			public void actionPerformed(ActionEvent arg0) {
				LookupPatient lp = new LookupPatient(mac, nurseSSN);
				lp.setVisible(true);
			}// End actionPerformed method for btnLookup
		});// End ActionListener addition
		btnLookup.setBounds(33, 289, 89, 23);
		add(btnLookup);
		
		separator_2 = new JSeparator();
		separator_2.setForeground(Color.GRAY);
		separator_2.setBounds(0, 400, 150, 2);
		add(separator_2);

		lblNewLabel = new JLabel(new ImageIcon("135Hospital-Administration-Velociraptors-Attack.jpg"));
		lblNewLabel.setBounds(10, 413, 130, 130);
		add(lblNewLabel);

		btnLogout = new JButton("Logout");
		btnLogout.addActionListener(new ActionListener() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			public void actionPerformed(ActionEvent arg0) {
				mac.setCurrentUser("", "");
				mac.setCurrentCard("Login");
				mac.setLoginSuccess(false);
		//		mac.reset();
			}// End actionPerformed method for btnLogout
		});// End ActionListener addition
		btnLogout.setBounds(33, 554, 89, 23);
		add(btnLogout);

		lblNurseInfo = new JTextPane();
		lblNurseInfo.setBackground(UIManager.getColor("Button.background"));
		lblNurseInfo.setEditable(false);
		lblNurseInfo.setBounds(10, 11, 130, 78);
		add(lblNurseInfo);

		scrollPane = new JScrollPane();
		scrollPane.setBounds(162, 33, 628, 567);
		add(scrollPane);
		
		JButton btnDailyappointment = new JButton("DailyAppointment");
		btnDailyappointment.addActionListener(new ActionListener() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			public void actionPerformed(ActionEvent e) {
				DailyAppointmentSheet Daily= new DailyAppointmentSheet(mac);
					Daily.setVisible(true);
			}
		});
		btnDailyappointment.setBounds(22, 137, 117, 23);
		add(btnDailyappointment);
	}

	/**
	 * The tableInit method initializes the table for use in the database.
	 */ 
	public void tableInit(){
		try {
			tblNursePatientList = new JTable(myModel());

			tblNursePatientList.setFillsViewportHeight(true);
			scrollPane.setViewportView(tblNursePatientList);

			tblNursePatientList.addMouseListener(new MouseAdapter() {
				/**
				 * Designed to perform the action specified upon triggering by MouseEvent.
				 * 
				 * @param arg0 The MouseEvent that triggers the method call.
				 */ 
				@Override
				public void mouseClicked(MouseEvent arg0) {
					if (arg0.getClickCount() == 2) {
						JTable target = (JTable)arg0.getSource();
						int row = target.getSelectedRow();
						try{
							chosenSSN = (Integer) target.getValueAt(row, 3);
							NursePatientView dpv = new NursePatientView(mac, chosenSSN, nurseSSN);
							dpv.setVisible(true);
						}catch(Exception e){}

					}
					if (arg0.getClickCount() == 1) {
						JTable target = (JTable)arg0.getSource();
						int row = target.getSelectedRow();
						try{
							chosenSSN = (Integer) target.getValueAt(row, 3);
						}// end try statement
						catch(Exception e){}
					}// end if statement
				}
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//			e.printStackTrace();
		}
	}// End MouseListener addition
	
	/**
	 * Set the Nurse text
	 * 
	 * @param firstName
	 * @param lastName
	 * @param currentUser
	 * @param SSN
	 */
	public void setInfo(String firstName, String lastName, String currentUser, int SSN) {
		this.nurseSSN = SSN;
		lblNurseInfo.setText(firstName+" "+lastName+"\n"+currentUser);		
	}
	
	/**
	 * myModel sets up the TableModel used in this class.
	 *
	 * @return model
	 */ 
	public TableModel myModel() throws Exception{
		TableModel model = new TableModel(mac);
		return model;
	}

	/**
	 * @return the tblDoctorPatientList
	 */
	public JTable getTblNursePatientList() {
		return tblNursePatientList;
	}

	/**
	 * @param tblDoctorPatientList the tblDoctorPatientList to set
	 */
	public void setTblNursePatientList(JTable tblNursePatientList) {
		this.tblNursePatientList = tblNursePatientList;
	}

	/**
	 * @return the chosenSSN
	 */
	public Integer getChosenSSN() {
		return chosenSSN;
	}

	/**
	 * @param chosenSSN the chosenSSN to set
	 */
	public void setChosenSSN(Integer chosenSSN) {
		this.chosenSSN = chosenSSN;
	}

	/**
	 * @return the drSSN
	 */
	public int getNurseSSN() {
		return nurseSSN;
	}

	/**
	 * @param drSSN the drSSN to set
	 */
	public void setDrSSN(int nurseSSN) {
		this.nurseSSN = nurseSSN;
	}
}
